<script setup>
import { ref, onMounted, getCurrentInstance, computed } from 'vue'
import { useRouter, useRoute } from 'vue-router'
import { usernameStore, cartCountStore } from '@/store/index.js'

const { proxy } = getCurrentInstance()
const prefix = import.meta.env.VITE_BASE_URL
const username = computed(() => usernameStore().username)
const cartCount = computed(() => cartCountStore().cartCount)
const { saveUserName } = usernameStore()
const { saveCartCount } = cartCountStore()
const dianshiItems = [
  {
    name: '小米壁画电视 65英寸',
    price: '6999元',
    imgSrc: '/imgs/nav-img/nav-3-1.jpg'
  },
  {
    name: '小米全面屏电视E55A',
    price: '1999元',
    imgSrc: '/imgs/nav-img/nav-3-2.jpg'
  },
  {
    name: '小米电视4A 32英寸',
    price: '699元',
    imgSrc: '/imgs/nav-img/nav-3-3.png'
  },
  {
    name: '小米电视5A 55英寸',
    price: '1799元',
    imgSrc: '/imgs/nav-img/nav-3-4.jpg'
  },
  {
    name: '小米电视4A 65英寸',
    price: '2699元',
    imgSrc: '/imgs/nav-img/nav-3-5.jpg'
  },
  {
    name: '查看全部',
    price: '小米电视',
    imgSrc: '/imgs/nav-img/nav-3-6.png'
  },
]
const phoneList = ref([])
const router = useRouter()
const route = useRoute()
onMounted(() => {
  getProductList()
  let params = route.params
  if (params && params.from === 'login') {
    getCartCount()
  }
})
const goToCart = () => {
  // 动态路由跳转需要使用router.params或router.query取参数
  router.push('/cart')
}
const getProductList = () => {
  proxy.axios.get('/products', {
    // params: {
    //   categoryId: '100012',
    //   pageSize: 6
    // }
  }).then((res) => {
    phoneList.value = res.list
  })
}
const getCartCount = () => {
  proxy.axios.get('/carts/products/sum').then((res = 0) => {
    saveCartCount(res)
  })
}
const goToLogin = () => {
  router.push('/login')
}
const goToLogout = () => {
  proxy.axios.post('/user/logout').then(() => {
    proxy.$message.success('退出成功')
    proxy.$cookies.remove('userId')
    saveUserName('')
    saveCartCount(0)
  })
}
</script>

<template>
  <div class="header">
    <div class="nav-topbar">
      <div class="container">
        <div class="topbar-menu">
          <router-link to="">小米商城</router-link>
          <router-link to="">MUI</router-link>
          <router-link to="">Iot</router-link>
          <router-link to="">云服务</router-link>
          <router-link to="">天星数科</router-link>
          <router-link to="">有品</router-link>
          <router-link to="">小爱开放平台</router-link>
          <router-link to="">企业团购</router-link>
          <router-link to="">资质证照</router-link>
          <router-link to="">协议规则</router-link>
          <router-link to="">下载App</router-link>
          <router-link to="">智能生活</router-link>
        </div>
        <div class="topbar-user">
          <router-link to="" v-if="username">{{ username }}</router-link>
          <router-link to="" v-else @click="goToLogin">登录</router-link>
          <router-link to="" v-if="username" @click="goToLogout">退出</router-link>
          <router-link to="/order/list" v-if="username">我的订单</router-link>
          <router-link to="" class="my-cart" @click="goToCart">
            <span class="icon-cart"></span>购物车({{ cartCount }})
          </router-link>
        </div>
      </div>
    </div>
    <div class="nav-header">
      <div class="container">
        <div class="header-logo">
          <router-link to="/index"></router-link>
        </div>
        <div class="header-menu">
          <div class="item-menu">
            <span>小米手机</span>
            <div class="children">
              <ul>
                <li class="product" v-for="(item, index) in dianshiItems" :key="index">
                  <a href="" target="_blank">
                    <div class="pro-img">
                      <img
                        src="https://cdn.cnbj1.fds.api.mi-img.com/mi-mall/c29ce8888fee7bf46aa56cfdb5367b06.png?thumb=1&w=200&h=138&f=webp&q=90"
                        alt="">
                    </div>
                    <div class="pro-name">小米CC9</div>
                    <div class="pro-price">1799元</div>
                  </a>
                </li>
              </ul>
            </div>
          </div>
          <div class="item-menu">
            <span>RedMi红米</span>
          </div>
          <div class="item-menu">
            <span>电视</span>
            <div class="children">
              <ul>
                <li class="product" v-for="(item, index) in dianshiItems" :key="index">
                  <a href="" target="_blank">
                    <div class="pro-img">
                      <img v-lazy="prefix + item.imgSrc" :data-img="item.imgSrc" alt="">
                    </div>
                    <div class="pro-name">{{ item.name }}</div>
                    <div class="pro-price">{{ item.price }}</div>
                  </a>
                </li>
              </ul>
            </div>
          </div>
          <div class="item-menu">
            <span>笔记本</span>
            <div class="children">
              <ul>
                <li class="product" v-for="(item, index) in dianshiItems" :key="index">
                  <a href="" target="_blank">
                    <div class="pro-img">
                      <img v-lazy="prefix + item.imgSrc" :data-img="item.imgSrc" alt="">
                    </div>
                    <div class="pro-name">{{ item.name }}</div>
                    <div class="pro-price">{{ item.price }}</div>
                  </a>
                </li>
              </ul>
            </div>
          </div>
          <div class="item-menu">
            <span>平板</span>
            <div class="children">
              <ul>
                <li class="product" v-for="(item, index) in dianshiItems" :key="index">
                  <a href="" target="_blank">
                    <div class="pro-img">
                      <img v-lazy="prefix + item.imgSrc" :data-img="item.imgSrc" alt="">
                    </div>
                    <div class="pro-name">{{ item.name }}</div>
                    <div class="pro-price">{{ item.price }}</div>
                  </a>
                </li>
              </ul>
            </div>
          </div>
          <div class="item-menu">
            <span>家电</span>
            <div class="children">
              <ul>
                <li class="product" v-for="(item, index) in dianshiItems" :key="index">
                  <a href="" target="_blank">
                    <div class="pro-img">
                      <img v-lazy="prefix + item.imgSrc" :data-img="item.imgSrc" alt="">
                    </div>
                    <div class="pro-name">{{ item.name }}</div>
                    <div class="pro-price">{{ item.price }}</div>
                  </a>
                </li>
              </ul>
            </div>
          </div>
          <div class="item-menu">
            <span>路由器</span>
            <div class="children">
              <ul>
                <li class="product" v-for="(item, index) in dianshiItems" :key="index">
                  <a href="" target="_blank">
                    <div class="pro-img">
                      <img v-lazy="prefix + item.imgSrc" :data-img="item.imgSrc" alt="">
                    </div>
                    <div class="pro-name">{{ item.name }}</div>
                    <div class="pro-price">{{ item.price }}</div>
                  </a>
                </li>
              </ul>
            </div>
          </div>
          <div class="item-menu">
            <span>服务中心</span>
            <div class="children">
              <ul>
                <li class="product" v-for="(item, index) in dianshiItems" :key="index">
                  <a href="" target="_blank">
                    <div class="pro-img">
                      <img v-lazy="prefix + item.imgSrc" :data-img="item.imgSrc" alt="">
                    </div>
                    <div class="pro-name">{{ item.name }}</div>
                    <div class="pro-price">{{ item.price }}</div>
                  </a>
                </li>
              </ul>
            </div>
          </div>
          <div class="item-menu">
            <span>社区</span>
            <div class="children">
              <ul>
                <li class="product" v-for="(item, index) in dianshiItems" :key="index">
                  <a href="" target="_blank">
                    <div class="pro-img">
                      <img v-lazy="prefix + item.imgSrc" :data-img="item.imgSrc" alt="">
                    </div>
                    <div class="pro-name">{{ item.name }}</div>
                    <div class="pro-price">{{ item.price }}</div>
                  </a>
                </li>
              </ul>
            </div>
          </div>
        </div>
        <div class="header-search">
          <div class="wrapper">
            <input type="text" name="keyword">
            <router-link to="/login"></router-link>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<style lang="scss">
@import url('@/assets/base.scss');
@import './../assets/config.scss';
@import './../assets/mixin.scss';

.header {
  .nav-topbar {
    height: 39px;
    line-height: 39px;
    background-color: #333333;
    color: #B0B0B0;

    .container {
      @include flex();
      font-size: 16px;

      a {
        display: inline-block;
        color: #B0B0B0;
        margin-right: 17px;
      }

      .my-cart {
        width: 110px;
        background-color: #FF6600;
        text-align: center;
        color: #ffffff;
        margin-right: 0;

        .icon-cart {
          @include bgImg(16px, 12px, '/imgs/icon-cart-checked.png');
          margin-right: 4px;
        }
      }
    }
  }
}

.nav-header {
  .container {
    height: 112px;
    @include flex();
    position: relative;
    z-index: 10;

    .header-menu {
      display: inline-block;
      padding-left: 209px;
      width: 643px;

      .item-menu {
        display: inline-block;
        color: #333333;
        font-weight: bold;
        font-size: 16px;
        line-height: 112px;
        margin-right: 20px;

        span {
          cursor: pointer;
        }

        &:hover {
          color: $colorA;

          .children {
            height: 220px;
            opacity: 1;
          }
        }

        .children {
          position: absolute;
          top: 112px;
          left: 0;
          width: 1226px;
          height: 0;
          opacity: 0;
          overflow: hidden;
          border-top: 1px solid #E5E5E5;
          box-shadow: 0px 7px 6px 0px rgba(0, 0, 0, 0.11);
          transition: all .5s;

          .product {
            position: relative;
            float: left;
            width: 16.66%;
            height: 220px;
            font-size: 12px;
            line-height: 12px;
            text-align: center;
            background-color: #ffffff;

            a {
              display: inline-block;
            }

            img {
              width: auto;
              height: 111px;
              margin-top: 26px;
            }

            .pro-img {
              height: 137px;
            }

            .pro-name {
              font-weight: bold;
              margin-top: 19px;
              margin-bottom: 8px;
              color: $colorB;
            }

            .pro-price {
              color: $colorA;
            }

            &::before {
              content: '';
              position: absolute;
              top: 28px;
              right: 0;
              border-left: 1px solid $colorF;
              height: 100px;
              width: 1px;
            }

            &:last-child::before {
              content: '';
              display: none;
            }
          }
        }
      }
    }

    .header-search {
      width: 319px;

      .wrapper {
        height: 50px;
        border: 1px solid #E0E0E0;

        input {
          border: none;
          border-right: 1px solid #E0E0E0;
          width: 264px;
          height: 50px;
          text-align: right;
          padding-right: 14px;
          box-sizing: border-box;
          /* 宽度包括内边距和边框 */
        }

        a {
          @include bgImg(18px, 18px, '/imgs/icon-search.png');
          margin-left: 17px;
        }
      }
    }
  }
}
</style>
